<?php
declare(strict_types=1);
/**
 * Created by PhpStorm
 * User: Administrator
 * Date: 2022/10/27
 * Time: 13:47:48
 */

namespace app\shop\controller;

use app\common\service\ChannelCouponService\ChannelCouponService;
use app\Normal;
use app\Request;
use think\annotation\Route;
use think\annotation\route\Group;
use think\annotation\route\Middleware;
use app\model\BbcModel\ChannelCoupon as ChannelCouponModel;

/**
 * 小B优惠券
 * @Group("ChannelCoupon")
 * @Middleware("{shopMiddileware::class}")
 */
class ChannelCoupon extends \app\BaseShopController
{
    public function initialize(){
        $time = time();
        ChannelCouponModel::instance()->edit([['status','=',1],['begindate','<=',$time],['is_expiration','=',2]],['status'=>2]);
        ChannelCouponModel::instance()->edit([['status','=',2],['enddate','<',$time],['is_expiration','=',2]],['status'=>3]);
    }
    /**
     * 优惠券列表
     * @Route("couponLists",method="GET")
     * @return void
     */
    public function couponLists( Request $request){
        $result = callApi(ChannelCouponService::class,"couponLists",$this->_all_data);
        return apiResponse($result);

    }

    /**
     * 新增优惠券
     * @Route("addChannelCoupon",method="POST")
     * @return void
     */
    public function addChannelCoupon(){
        $result = callApi(ChannelCouponService::class,"addCoupon",$this->_all_data);
        if($result['code'] == Normal::SUC){
            $this->log(['content'=>'新增名称【' . $this->_all_data['name'] .'】的小B优惠券','request_param'=> json_encode($this->_all_data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }

    /**
     * 优惠券详情
     * @Route("couponInfo/:id",method="GET")
     * @return void
     */
    public function couponInfo(){
        $result = callApi(ChannelCouponService::class,"couponInfo",$this->_all_data);
        return apiResponse($result);
    }

    /**
     * 终止优惠券
     * @Route("changeStatus",method="POST")
     * @return void
     */
    public function changeStatus(){
        $result = callApi(ChannelCouponService::class,"changeStatus",$this->_all_data);
        if($result['code'] == Normal::SUC){
            $this->log(['content'=>'结束ID为【'.$this->_all_data['id'] . '】的小B优惠券活动','request_param'=> json_encode($this->_all_data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
        }
        return apiResponse($result);
    }
}